package com.hellogood.yaohuo.server.mgr;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

import com.hellogood.yaohuo.server.dao.factory.DAOFactory;
import com.hellogood.yaohuo.shared.data.Constants;
import com.hellogood.yaohuo.shared.entity.Org;
import com.hellogood.yaohuo.shared.entity.Quyu;
import com.hellogood.yaohuo.shared.entity.QuyuOrg;
import com.hellogood.yaohuo.shared.vo.QuyuVo;

public class QuyuOrgMgr {

	public String addOrgToQuyu(Quyu qy, Org org) {
		return DAOFactory.getInstance().createQuyuOrgDAO().addOrgToQuyu(qy, org);
	}

	public String removeOrgFromQuyu(Org org) {
		return DAOFactory.getInstance().createQuyuOrgDAO().removeOrgFromQuyu(org);
	}

	public String addOrgToQuyu(List<Org> orgs, String qyCode) {
		return DAOFactory.getInstance().createQuyuOrgDAO().addOrgToQuyu(orgs, qyCode);
	}

	public String updateOrgToQuyu(List<String> codes, String quyuCode) {
		String result = DAOFactory.getInstance().createQuyuOrgDAO().removeOrgFromQuyu(quyuCode);
		if (result.equals("")) {
			result = DAOFactory.getInstance().createQuyuOrgDAO().addOrgCodeToQuyu(codes, quyuCode);
		}
		return result;
	}

	public String removeOrgFromQuyu(List<String> orgCodeList) {
		return DAOFactory.getInstance().createQuyuOrgDAO().removeOrgFromQuyu(orgCodeList);
	}

	public boolean doesQuyuHasOrg(String quyuCode) {
		List<QuyuOrg> list = DAOFactory.getInstance().createQuyuOrgDAO().getQuyuOrgByQuyuCode(quyuCode);
		if (list != null && list.size() > 0) {
			return true;
		} else {
			return false;
		}
	}

	public List<QuyuVo> getQuyuOrg() {
		List<QuyuVo> result = new ArrayList<QuyuVo>();
		List<QuyuOrg> list = DAOFactory.getInstance().createQuyuOrgDAO().getQuyuOrg();
		List<Quyu> qyList = DAOFactory.getInstance().createQuyuDAO().getAllQy();
		Map<String, String> qyMap = new HashMap<String, String>();
		for (Quyu qy : qyList) {
			qyMap.put(qy.getQyCode(), qy.getQyName());
		}

		if (list != null) {
			Map<String, String> map = new HashMap<String, String>();
			for (QuyuOrg q : list) {
				QuyuVo vo = new QuyuVo();
				vo.setNodeId(q.getQyCode() + "-" + q.getOrgCode());
				vo.setCode(q.getOrgCode());
				vo.setName(q.getOrgName());
				vo.setParentCode(q.getQyCode());
				vo.setOpen(false);
				result.add(vo);
				if (!map.containsKey(q.getQyCode())) {
					map.put(q.getQyCode(), q.getQyName());
				}
			}
			Set<String> set = map.keySet();
			Iterator<String> it = set.iterator();
			while (it.hasNext()) {
				QuyuVo vo = new QuyuVo();
				String code = it.next();
				vo.setNodeId(code);
				vo.setCode(code);
				vo.setName(qyMap.get(code));
				vo.setOpen(false);
				vo.setParentCode("0");
				result.add(vo);
			}
			QuyuVo vo = new QuyuVo();
			vo.setCode("0");
			vo.setName("");
			vo.setOpen(true);
			vo.setParentCode("r");
			result.add(vo);
		}
		return result;
	}

	public List<QuyuOrg> getQuyuOrgByCode(String code) {
		return DAOFactory.getInstance().createQuyuOrgDAO().getQuyuOrgByCode(code);
	}

	public List<QuyuVo> getQuyuVoOrg(String staffCode) {
		List<QuyuVo> result = new ArrayList<QuyuVo>();
		List<QuyuOrg> list = DAOFactory.getInstance().createQuyuOrgDAO().getQuyuOrgByCode(staffCode);
		if (staffCode.equals(Constants.ADMINISTRATOR_CODE)) {
			list = DAOFactory.getInstance().createQuyuOrgDAO().getQuyuOrg();
		}
		List<Quyu> qyList = DAOFactory.getInstance().createQuyuDAO().getAllQy();
		Map<String, String> qyMap = new HashMap<String, String>();
		for (Quyu qy : qyList) {
			qyMap.put(qy.getQyCode(), qy.getQyName());
		}

		if (list != null) {
			Map<String, String> map = new HashMap<String, String>();
			for (QuyuOrg q : list) {
				QuyuVo vo = new QuyuVo();
				vo.setNodeId(q.getQyCode() + "-" + q.getOrgCode());
				vo.setCode(q.getOrgCode());
				vo.setName(q.getOrgName());
				vo.setParentCode(q.getQyCode());
				vo.setOpen(false);
				result.add(vo);
				if (!map.containsKey(q.getQyCode())) {
					map.put(q.getQyCode(), q.getQyName());
				}
			}
			Set<String> set = map.keySet();
			Iterator<String> it = set.iterator();
			while (it.hasNext()) {
				QuyuVo vo = new QuyuVo();
				String code = it.next();
				vo.setNodeId(code);
				vo.setCode(code);
				vo.setName(qyMap.get(code));
				vo.setOpen(false);
				vo.setParentCode("0");
				result.add(vo);
			}
		}
		return result;
	}

}
